﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Comm;
using VehicleMaintenance.Models;
using VehicleMaintenance.BaseInfo;
using VehicleMaintenance.Events;

namespace VehicleMaintenance.Transaction
{
	public partial class DamageBillForm : AddOrEditBaseForm
	{

		/// <summary>
		/// 记录当前ID
		/// </summary>
		private int billID = 0;


		public DamageBillForm()
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.DamageBill));
			InitializeSepcial();
		}

		public DamageBillForm(int id)
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.DamageBill));
			InitializeSepcial();

			billID = id;
			state = State.Edit;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				CarDBContext db = new CarDBContext();

				if (!ucFormInput1.VerifyInput())
				{
					return;
				}

				if (this.state == State.Edit)
				{
					DamageBill bill = db.DamageBill.First(p => p.ID == billID);
					int oldNumber = bill.Number;
					ucFormInput1.GetValues(bill);
					int newNumber = bill.Number;
					db.Goods.Find(bill.GoodID).Number -= (newNumber - oldNumber);
					db.SaveChanges();

					this.ShowInfo("保存成功！");
					this.DialogResult = System.Windows.Forms.DialogResult.OK;
				}
				else
				{
					var bill = db.DamageBill.Create();
					ucFormInput1.GetValues(bill);
					//更新库存
					db.Goods.Find(bill.GoodID).Number -= Convert.ToInt32(ucFormInput1.GetControlByName("Number").Text);

					db.DamageBill.Add(bill);
					db.SaveChanges();

					if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						ucFormInput1.ClearData();
						InitializeDefault();
						ucFormInput1.GetControlByName("Code").Focus();
					}
					else
					{
						this.DialogResult = System.Windows.Forms.DialogResult.OK;
					}
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void DamageBillForm_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.state == State.Edit)
				{
					CarDBContext db = new CarDBContext();

					DamageBill bill = db.DamageBill.Include("Good").Include("Staff").First(p => p.ID == this.billID);
					ucFormInput1.SetValues(bill);

					ucFormInput1.GetControlByName("GoodID").Text = bill.Good.Code;
					ucFormInput1.GetControlByName("StaffID").Text = bill.Staff.Name;
				}
				else
				{
					ucFormInput1.ClearData();

					InitializeDefault();

				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void InitializeSepcial()
		{
			try
			{
				ucFormInput1.GetControlByName("GoodID").Click += new EventHandler(good_Click);
				ucFormInput1.GetControlByName("GoodID").BackColor = Color.LightGray;
				ucFormInput1.GetControlByName("StaffID").Click += new EventHandler(staff_Click);
				ucFormInput1.GetControlByName("StaffID").BackColor = Color.LightGray;
				ucFormInput1.GetControlByName("Number").KeyPress += new KeyPressEventHandler(KeyEvents.Number_KeyPress);
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void good_Click(object sender, EventArgs e)
		{
			try
			{

				TextBox txt = (sender as TextBox);

				GoodListForm form = new GoodListForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.Good.Code;
					txt.Tag = form.Good.GoodID;

					ucFormInput1.GetControlByName("GoodName").Text = form.Good.Name;
					ucFormInput1.GetControlByName("Units").Text = form.Good.Units;
				}


			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void staff_Click(object sender, EventArgs e)
		{
			try
			{
				TextBox txt = (sender as TextBox);

				StaffListForm form = new StaffListForm(true);
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.Staff.Name;
					txt.Tag = form.Staff.StaffID;
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void InitializeDefault()
		{
			CarDBContext db = new CarDBContext();
			string str = DateTime.Now.ToString("yyyyMMdd");
			DamageBill bill = db.DamageBill.OrderByDescending(p => p.ID).FirstOrDefault(p => p.Code.StartsWith(str));
			if (bill == null)
			{
				str += "001";
			}
			else
			{
				str = (Convert.ToInt64(bill.Code) + 1).ToString();
			}
			ucFormInput1.GetControlByName("Code").Text = str;
		}
	}
}
